<?php
namespace Acme\Test\EventListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use RMD\AccessBundle\Entity\User;
use RMD\AccessBundle\Entity\TheList;
//use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\DependencyInjection\ContainerInterface;

class TestListener
{
	protected $container;
	protected $tables;

	public function __construct(ContainerInterface $container, $tables)
	{
		$this->container = $container;
		$this->tables = $tables;
	}

	public function loadClassMetadata(LoadClassMetadataEventArgs $args)
	{
		$metadata = $args->getClassMetadata();
		$tableName = $metadata->getTableName();
		if (in_array($tableName, $this->tables)) {
			if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {

				//var_dump($this->container->get('security.context')->getToken()->getUser()->getId());
				$metadata->setTableName($this->container->get('security.context')->getToken()->getUser()->getId() . '_' . $tableName);
				//var_dump($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY'));
				//$metadata->setTableName($this->container->get('security.context')->getToken()->getUser()->getId() . '_' . $tableName);
				//error_log($this->securityContext->getToken()->getUser() . "\n", 3, '/var/www/symfony/app/logs/custom.log');
			}
		}
	}
}
